package com.hk.influx.dsl.functions.predicate;

import com.influxdb.query.dsl.functions.properties.FunctionsParameters;
import com.influxdb.utils.Arguments;

import javax.annotation.Nonnull;


/**
 * @author Kevin
 */
public class ColumnDeletePredicates extends DeletePredicates {

    private final String fieldName;

    ColumnDeletePredicates(String fieldName) {
        Arguments.checkNonEmpty(fieldName, "fieldName column");
        this.fieldName = fieldName;
    }

    /**
     * =
     */
    public DeletePredicates eq(Object value) {
        return new OperatorDeletePredicates(fieldName, "=", value);
    }

    /**
     * !=
     */
    public DeletePredicates notEqual(Object value) {
        return new OperatorDeletePredicates(fieldName, "!=", value);
    }

    private static class OperatorDeletePredicates extends DeletePredicates {
        private final String fieldName;
        private final Object fieldValue;
        private final String operator;

        private OperatorDeletePredicates(@Nonnull final String fieldName,
                                         @Nonnull final String operator,
                                         @Nonnull final Object fieldValue) {
            this.fieldName = fieldName;
            this.fieldValue = fieldValue;
            this.operator = operator;
        }

        @Override
        public String toString() {
            String val;
            if (fieldValue instanceof String) {
                val = STR."\"\{fieldValue}\"";
            } else {
                val = FunctionsParameters.serializeValue(fieldValue, true);
            }
            return this.fieldName + this.operator + val;
        }
    }
}
